package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.fans;

import android.os.Bundle;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.PersonFansUrl)
public class FansActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tab)
    CommonTabLayout tab;
    // @BindView(R.id.vp_content)
    //ViewPager2 vpContent;

    String type1Title = "关注";
    String type2Title = "粉丝";

    @Autowired
    int type = 0;
    @Autowired
    int userId = -1;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_fans_list;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        //tvTitle.setText("Fans");
        String tabText1 = String.format("%s%s", type1Title, 0);
        String tabText2 = String.format("%s%s", type2Title, 0);
        ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();
        tabEntities.add(new CustomTabEntity() {
            @Override
            public String getTabTitle() {
                return tabText1;
            }

            @Override
            public int getTabSelectedIcon() {
                return 0;
            }

            @Override
            public int getTabUnselectedIcon() {
                return 0;
            }
        });
        tabEntities.add(new CustomTabEntity() {
            @Override
            public String getTabTitle() {
                return tabText2;
            }

            @Override
            public int getTabSelectedIcon() {
                return 0;
            }

            @Override
            public int getTabUnselectedIcon() {
                return 0;
            }
        });

        ArrayList<Fragment> fragmentList = new ArrayList<>();
        FansFragment fansFragment01 = FansFragment.newInstance(1, userId);
        FansFragment fansFragment02 = FansFragment.newInstance(2, userId);
        fragmentList.add(fansFragment01);
        fragmentList.add(fansFragment02);

        fansFragment01.setOnAttentionChangedEvent(onChangedEvent);
        fansFragment02.setOnAttentionChangedEvent(onChangedEvent);

        tab.setTabData(tabEntities, this, R.id.fl_content, fragmentList);

        tab.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position == 0) {
                    tvTitle.setText("关注列表");
                } else {
                    tvTitle.setText("粉丝列表");
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tab.setCurrentTab(type);
        if (type == 0) {
            tvTitle.setText("关注列表");
        } else {
            tvTitle.setText("粉丝列表");
        }
    }

    private FansFragment.OnAttentionChangedEvent onChangedEvent = (count, type) -> {
        if (type == 1) {
            tab.getTitleView(0).setText(String.format("%s%s", type1Title, count));
        } else {
            tab.getTitleView(1).setText(String.format("%s%s", type2Title, count));
        }
    };
}
